﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace DSLFactory.VSCTDesigner
{
    internal static class GuidSymbolHelper
    {
        /// <summary>
        /// Ensures the ID symbol.
        /// </summary>
        /// <param name="guid">The GUID.</param>
        /// <param name="value">The value.</param>
        public static void EnsureIDSymbol(CommandTable commandTable, GuidSymbol guid, string value)
        {
            if( String.IsNullOrEmpty(value) )
                return;

            if( commandTable.NativeCommandTable != null )
            {
                var ids = commandTable.NativeCommandTable.GetIDSymbols(new Guid(guid.Value));
                if( ids != null && ids.Contains(value) )
                    return;
            }

            // Recherche si cette valeur existe
            if( !( from id in guid.IDSymbols
                   where id.Name == value
                   select id ).Any() )
            {
                // Création
                IDSymbol id = new IDSymbol(guid.Store);
                id.Name = value;
                guid.IDSymbols.Add(id);
            }
        }

        internal static uint ParseID(string value)
        {
            try
            {
                if( value.StartsWith("0x", StringComparison.OrdinalIgnoreCase) )
                {
                    return uint.Parse(value.Substring(2), NumberStyles.HexNumber);
                }
                else
                {
                    return uint.Parse(value);
                }
            }
            catch {
                throw new Exception("Invalid value for id. Must be an integer");
            }
        }

        public static void RemoveIDSymbol(GuidSymbol guid, string value)
        {
            if( !String.IsNullOrEmpty(value) && guid.IDSymbols.Count > 0)
            {
                var idSymbol = ( from id in guid.IDSymbols
                                 where id.Name == value
                                 select id ).FirstOrDefault();
                if( idSymbol != null )
                    guid.IDSymbols.Remove(idSymbol);
            }
        }

        internal static void EnsureIDSymbol(Bitmap bitmap, string id)
        {
            GuidSymbol gs = EnsureGuidSymbol(bitmap.Bitmaps.CommandTable, bitmap.Guid);
            EnsureIDSymbol(bitmap.Bitmaps.CommandTable, gs, id);
        }

        internal static void RemoveGuidSymbol(CommandTable commandTable, string guidName)
        {
            GuidSymbol gs = FindGuidSymbol(commandTable, guidName);
            if( gs!=null)
                gs.Delete();
        }

        internal static void RemoveIDSymbol(Bitmap bitmap, string id)
        {
            GuidSymbol gs = EnsureGuidSymbol(bitmap.Bitmaps.CommandTable, bitmap.Guid);
            RemoveIDSymbol(gs, id);
        }

        internal static GuidSymbol EnsureGuidSymbol(CommandTable commandTable, string guidName)
        {
            GuidSymbol gs = FindGuidSymbol(commandTable, guidName);
            if( gs == null )
            {
                gs = new GuidSymbol(commandTable.Store);
                gs.Name = guidName;

                Guid g = commandTable.NativeCommandTable.FindSymbolGuid(guidName);
                if( g == Guid.Empty )
                    g = Guid.NewGuid();
                gs.Value = g.ToString("B");
                commandTable.Symbols.Add(gs);
            }
            return gs;
        }

        /// <summary>
        /// Finds the GUID symbol.
        /// </summary>
        /// <param name="commandTable">The command table.</param>
        /// <param name="guidName">Name of the GUID.</param>
        /// <returns></returns>
        internal static GuidSymbol FindGuidSymbol(CommandTable commandTable, string guidName)
        {
            if( commandTable == null || commandTable.Symbols.Count == 0)
                return null;

            return ( from guid in commandTable.Symbols
                     where guid.Name == guidName
                     select guid ).FirstOrDefault();
        }


        internal static void RemoveIDSymbol(CommandTable commandTable, string guidName, string id)
        {
            GuidSymbol gs = FindGuidSymbol(commandTable, guidName);
            if( gs != null )
                RemoveIDSymbol(gs, id);
        }
    }
}
